package InsLoginRegister;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet",urlPatterns ="/insRegister")
public class RegisterServlet extends HttpServlet {
    java.sql.Connection conn = null;
    String strConn;
    java.sql.Statement sqlStmt = null,sqlStmt1 = null;
    java.sql.ResultSet sqlRst = null,sqlRst1=null;
    long M = 111111 ;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
       try
       {
           Class.forName("org.h2.Driver");
           conn = java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1", "sa", "sa");
           sqlStmt = conn.createStatement();
           sqlStmt1 = conn.createStatement();
                 String NAME= request.getParameter("name");
                 String PNO1 = request.getParameter("pno");
                 String SEX = request.getParameter("sex");
                 String BIRTHDAY = request.getParameter("birthday");
                 String PHONE = request.getParameter("phone");
                 String  sql = "select MNO from MANAGER";
                 sqlRst  = sqlStmt.executeQuery(sql);
                 while(sqlRst.next()) M++; //初始111111为经理员工编号，后每增加一个员工，员工编号依此加 1
                 //初始密码为000000，后面需员工自行改密码
                 String sql1 ="INSERT INTO MANAGER VALUES("+"'"+M+"',"+"'"+PNO1+"',"+"'"+NAME+"',"+"'"+SEX+"',"+"'"+BIRTHDAY+"',"+"'000000'"+","+"'"+PHONE+"')";
                 String sql2=null;
              if(PNO1.equals("123456")) {
                    sql2 = "insert into salary(mno,salary)values("+"'"+M+"',"+"'5000')";
                 }
           else   sql2 = "insert into salary(mno,salary)values("+"'"+M+"',"+"'3500')";
                sqlStmt1.executeUpdate(sql2);
                sqlStmt.executeUpdate(sql1);
                response.sendRedirect("http://localhost:8084/html/index.jsp");
       }
       catch (Exception ex)
       {
           response.getWriter().write(ex.toString());
       }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
